<?php namespace App\Http\Controllers\Api;

use App\CoinLog;
use App\Http\Controllers\Controller;
use App\Signed;
use App\User;
use App\UserAddress;
use App\UserInfo;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    public function setUserAddress(Guard $guard,Request $request){
        $id        = !is_null($request->get('id')) ? $request->get('id') : 0;
        $realname  = $request->get('realname');
        $phone     = $request->get('phone');
        $district  = $request->get('district');
        $info      = $request->get('info');
        $isDefault = !is_null($request->get('is_default')) ? $request->get('is_default') : 0;

        $districtArr = explode(" ",$district);

        if($guard->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        /** @var User $user */
        $user = $guard->user();

        $thatAddress = UserAddress::whereUserId($user->id)->whereId($id)->first();
        if(!$thatAddress){
            $thatAddress = new UserAddress();
            $thatAddress->user_id = $user->id;
        }

        if(!is_null($realname))$thatAddress->realname = $realname;
        if(!is_null($phone))$thatAddress->phone = $phone;
        if(!is_null($district))$thatAddress->province = $districtArr[0];
        if(!is_null($district))$thatAddress->city = $districtArr[1];
        if(!is_null($district))$thatAddress->area = $districtArr[2];
        if(!is_null($info))$thatAddress->info = $info;
        $thatAddress->is_default = $isDefault;
        $thatAddress->save();

        return response()->json(['level'=>'success','message'=>'设置成功','data'=>$thatAddress]);
    }

    public function delUserAddress(Guard $guard,Request $request){
        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        if($guard->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);
        /** @var User $user */
        $user = $guard->user();

        /** @var UserAddress $thatAddress */
        $thatAddress = UserAddress::whereUserId($user->id)->whereId($id)->first();

        if(!$thatAddress)
            return response()->json(['level'=>'error','message'=>'不存在当前地址']);

        $thatAddress->delete();

        return response()->json(['level'=>'success','message'=>'删除成功']);
    }

    public function qianDao(Guard $guard,Request $request){
        if($guard->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);
        /** @var User $user */
        $user = $guard->user();

        $todayTime = strtotime(date('Y-m-d',time()));

        /** @var Signed $thatSigned */
        $thatSigned = Signed::whereUserId($user->id)->whereSignedTime($todayTime)->first();

        if($thatSigned)
            return response()->json(['level'=>'error','message'=>'今天您已经签到了！']);

        $thatSigned = new Signed();
        $thatSigned->user_id = $user->id;
        $thatSigned->signed_time = $todayTime;
        $thatSigned->save();

        //波罗币增加
        $userInfo = UserInfo::whereUserId($user->id)->first();
        if($userInfo){
            $userInfo->coin = (int)$userInfo->coin + 2;
            $userInfo->save();

            $coinLog = new CoinLog();
            $coinLog->coin = 2;
            $coinLog->desc = '签到成功';
            $coinLog->user_id = $user->id;
            $coinLog->save();
        }

        return response()->json(['level'=>'success','message'=>'完成签到','data'=>$todayTime]);
    }

    public function getMySigneds(Guard $guard,Request $request){
        if($guard->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);
        /** @var User $user */
        $user = $guard->user();

        $beginDate = strtotime(date("Y-m-01"));
        $BeginDate = date('Y-m-01', strtotime(date("Y-m-d")));
        $endDate   = strtotime("$BeginDate +1 month -1 day");

        $mySigneds = Signed::whereUserId($user->id)->whereBetween('signed_time',[$beginDate,$endDate])->get();
//        var_dump($endDate);die;

        $daysArr = array();
        if(count($mySigneds)){
            foreach ($mySigneds as $mySigned){
                /** @var Signed $mySigned */
                $day['signDay'] = date('d',$mySigned->signed_time);
                array_push($daysArr,$day);
            }
        }

        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$daysArr]);
    }

    public function setUserInfo(Guard $guard,Request $request){
        $type    = $request->get('type');
        $content = $request->get('content');

        if($guard->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);
        /** @var User $user */
        $user = $guard->user();

        /** @var UserInfo $thatUserInfo */
        $thatUserInfo = UserInfo::whereUserId($user->id)->first();

        if($thatUserInfo){
            switch ($type){
                case 'nickname':
                    $thatUserInfo->nickname = $content;
                    break;
                case 'realname':
                    $thatUserInfo->realname = $content;
                    $title = '修改姓名';
                    break;
                case 'qq':
                    $thatUserInfo->qq = $content;
                    $title = '修改QQ';
                    break;
                case 'wechat':
                    $thatUserInfo->wechat = $content;
                    $title = '修改微信号';
                    break;
            }

            $thatUserInfo->save();
        }

        return response()->json(['level'=>'success','message'=>'设置完成','data'=>$thatUserInfo]);
    }

    public function changePassword(Guard $guard,Request $request)
    {
        if($guard->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);
        /** @var User $user */
        $user = $guard->user();

        $oldPassword = $request->get('old_password');
        $newpassword = $request->get('new_password');

        if(!Hash::check($oldPassword,$user->password))
            return response()->json(['level'=>'error','message'=>'旧密码错误']);

        $user->password = bcrypt($newpassword);
        $user->save();

        $guard->logout();

        return response()->json(['level'=>'success','message'=>'修改成功']);
    }
}